<?php
/**
 * Created by PhpStorm.
 * User: nhson219
 * Date: 8/26/14
 * Time: 1:55 PM
 */
$bid = $bid->row();

?>
<div id="loading-image" style="display: none">
    <img src="<?php echo image_url('loading.gif') ?>"/>
</div>
<form id="form-edit-bid" method="POST">
    <div class="col-lg-12 col-md-12 col-sm-12 no-padding">
        <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 bid-step bid-step-1">
            <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12 bid-group">
                <span class="unit-price">$</span>
                <input data-toggle="tooltip" data-original-title="<?php echo $this->lang->line('step-1-tooltip')?>" name="bidAmt" id="bidAmt" autocomplete="off" type="text"
                       value="<?php if (isset($bid->bid_amount)) echo $bid->bid_amount; else echo set_value('bidAmt'); ?>"
                       class="bid-tooltip form-control">

            </div>
            <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12 x8-16 label-bid"><?php echo $this->lang->line("Bid Price")?></div>
        </div>
        <div class="col-lg-3 col-sm-6 col-md-3 col-xs-12 bid-step bid-step-2">
            <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12 bid-group">
                <span class="unit-price"><?php echo $this->lang->line("days")?></span>
                <input data-toggle="tooltip" data-original-title="<?php echo $this->lang->line('step-2-tooltip')?>" type="text"
                       value="<?php if (isset($bid->bid_days)) echo $bid->bid_days; else echo set_value('days'); ?>"
                       name="days" class="bid-tooltip form-control">
            </div>
            <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12 x8-16 label-bid"><?php echo $this->lang->line("Time of Bid")?></div>
        </div>
        <div class="col-lg-3 col-sm-6 col-md-3 col-xs-12 bid-step bid-step-3">
            <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12 bid-group">
                <span class="unit-price">$</span>
                <input data-toggle="tooltip" data-original-title="<?php echo $this->lang->line('step-3-tooltip')?>" type="text" id="escrow_flag" name="escrow_flag" value="<?php if (isset($bid->escrow_flag)) echo $bid->escrow_flag; else echo set_value('escrow_flag'); ?>" class="bid-tooltip form-control">
            </div>
            <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12 x8-16 label-bid"><?php echo $this->lang->line("escrow_flag")?></div>
        </div>
        <div class="col-lg-3 col-sm-6 col-md-3 col-xs-12 bid-step bid-step-4">
            <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12 bid-group">
                <input type="hidden" value="<?php if(isset($bid->id)) echo $bid->id?>" name="bidId" />


                    <button data-toggle="tooltip" data-original-title="<?php echo $this->lang->line('step-4-tooltip')?>" type="submit" name="postBid" value="submit" class="bid-tooltip col-lg-12 col-sm-12 col-md-12 col-xs-12 btn btn-bid btn-lg"><i class="icon-bid"></i>
                    </button>

            </div>
            <div id="button-bid" class="col-lg-12 col-sm-12 col-md-12 col-xs-12 x8-16 label-bid"><?php echo $this->lang->line("Place Bid")?></div>
        </div>
    </div>
    <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12 no-padding line-bid-receive">
        <div class="bid-receive">
            <div style="display:inline-block;background: #f9f9f9;padding: 0 10px;">
                <?php echo $this->lang->line("received")?> <span class="unit-price">$</span>
		        	<span id="bid-receive">
                        <?php if (isset($bid->bid_paid)) echo $bid->bid_paid; ?>
		            </span>

                <input type="hidden" name="bid_paid" id="bid_paid" value="<?php if (isset($bid->bid_paid)) echo $bid->bid_paid; ?>">
            </div>
        </div>
    </div>
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 no-padding">
        <div class="col-md-12 no-padding quote-first-35"></div>
        <div class="col-md-12 quote-content">
            <?php echo $this->lang->line("quote")?>
        </div>
        <div class="col-md-12 no-padding quote-last-35"></div>
    </div>
    <div class="col-lg-12 col-sm-12 col-md-12 col-xs-12 no-padding bid-comment">
        <textarea class="form-control" name="bid_desc" rows="5" placeholder="<?php echo $this->lang->line("placeholder_bid_comment")?>">
            <?php if (isset($bid->bid_desc)) echo $bid->bid_desc; ?>

        </textarea>
    </div>
</form>

<script>
    $("#bidAmt").on('input',function(){
        $("#bid-receive").html($("#bidAmt").val() - 5);
        $("#bid_paid").val($("#bidAmt").val() - 5);
    });
    $.validator.addMethod('lessThan',function(value,element,param){
        if(this.optional(element)) return true;
        var j = parseInt(value);
        var i = parseInt($('#bidAmt').val());
        return j < i;
    },'Yêu cầu đặt cọc phải nhỏ hơn giá bid');
    $("#form-edit-bid").validate({
        rules:{
            "bidAmt" : {
                required : true,
                digits : true,
                min: 5
            },
            "days" : {
                required : true,
                digits : true
            },
            "escrow_flag" : {
                required : true,
                digits : true,
                lessThan : $('#escrow_flag').val()
            },
            "bid_desc" : {
                required : true
            }
        },
        messages:{
            "bidAmt":{
                required : "<?php echo $this->lang->line('Your Bid')." ".$this->lang->line('required') ?>",
                digits : "<?php echo $this->lang->line('Your Bid')." ".$this->lang->line('numeric') ?>",
                min : "<?php echo $this->lang->line('Your Bid')." ".$this->lang->line('min')?>5"
            },
            "days":{
                required : "<?php echo $this->lang->line('Days')." ".$this->lang->line('required') ?>",
                digits : "<?php echo $this->lang->line('Days')." ".$this->lang->line('numeric') ?>"
            },
            "escrow_flag":{
                required : "<?php echo $this->lang->line('Milestone Request')." ".$this->lang->line('required') ?>",
                digits : "<?php echo $this->lang->line('Milestone Request')." ".$this->lang->line('numeric') ?>"
            },
            "bid_desc":{
                required : "<?php echo $this->lang->line('Bid desc')." ".$this->lang->line('required') ?>"
            }
        },
        showErrors: function(errorMap,errorList){
            $.each(this.successList,function(index,value){
                return $(value).popover('hide');
            });
            return $.each(errorList,function(index,value){
                var _popover;
                _popover = $(value.element).popover({
                    trigger: "manual",
                    placement: "bottom",
                    content: value.message,
                    template: "<div class=\"popover\"><div class=\"arrow\"></div><div class=\"popover-inner\"><div class=\"popover-content\"><p></p></div></div></div>"
                });
                _popover.data("bs.popover").options.content = value.message;
                return $(value.element).popover("show");
            });
        },
        submitHandler:function(form){
            $.ajax({
                type: "POST",
                url: "<?php echo base_url()."index.php/project/editBid"?>",
                data: $("#form-edit-bid").serializeArray(),
                beforeSend: function () {
                    $('#loading-image').show();
                },
                success: function (data) {
                    $('#loading-image').hide();
                    data = $.parseJSON(data);
                    if(data.status == 'error')
                    {
                        $('#error-bid').html(data.message);
                    }else{
                        //console.log(data.id);
                        //console.log(data.id[0].bid_amount);
                        $(".bid").load('<?php echo base_url()."index.php/project/editBid"?>',{'project_id':<?php echo $project->id;?>});
                    }
                }
            });
        }
    });
</script>